import React, { useState } from 'react';
import { Button, message, Modal, Spin } from 'antd';
import axios from '@/components/service/request.js';

const ParseFileText = ({ filePath,onParseSuccess  }) => {
  const [loading, setLoading] = useState(false);
  const [loadingText, setLoadingText] = useState('');

  const handleParse = async () => {
    if (!filePath) {
      return message.error('请先上传文件');
    }

    setLoading(true);
    setLoadingText('正在解析文件，请稍候...');

    try {
      console.log('开始解析文件');
      await axios.get('/model/run', { params: { filePath } });
      message.success('文件解析成功');
      onParseSuccess(); // 通知父组件更新状态
    } catch (error) {
      console.error('操作失败:', error);
      message.error('操作失败，请重试');
    } finally {
      setLoading(false); // 关闭遮罩
      setLoadingText('');
    }
  };

  return (
    <>
    <span 
      style={{ color: '#1890ff', cursor: loading ? 'not-allowed' : 'pointer' }} 
      onClick={!loading ? handleParse : undefined}
    >
      去解析
    </span>
      {/* 全屏遮罩和加载动画 */}
      <Modal open={loading} footer={null} closable={false} maskClosable={false} centered>
        <Spin tip={loadingText} size="large" /><p>正在解析文件，请稍候···</p>
      </Modal>
    </>
  );
};

export default ParseFileText;
